package com.zr.daoimpl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.zr.dao.SerachUsersByInfoDao;
import com.zr.model.Cuser;

@Repository("serachUsersByInfoDao")
public class SerachUsersByInfoDaoImpl<T> extends BaseDaoImpl<T> implements SerachUsersByInfoDao<T>{

	@Override
	public List<Cuser> getUsersByInfo(int page, int rows, String searType, String sertInfo) {
		List<Cuser> us = new ArrayList<Cuser>();
		if(searType.equals("uid")){
			String hql = "from Cuser where uid like :sertInfo";
			Query que = this.getCurrentSession().createQuery(hql);
			que.setString("sertInfo",'%'+sertInfo+'%');
			que.setFirstResult((page-1)*rows);
			que.setMaxResults(rows);
			us = que.list();
			return us;
		}else if(searType.equals("uname")){
			String hql = "from Cuser where uname like :sertInfo";
			Query que = this.getCurrentSession().createQuery(hql);
			que.setString("sertInfo",'%'+sertInfo+'%');
			que.setFirstResult((page-1)*rows);
			que.setMaxResults(rows);
			us = que.list();
			return us;
		}else if(searType.equals("utel")){
			String hql = "from Cuser where utel like :sertInfo";
			Query que = this.getCurrentSession().createQuery(hql);
			que.setString("sertInfo",'%'+sertInfo+'%');
			que.setFirstResult((page-1)*rows);
			que.setMaxResults(rows);
			us = que.list();
			return us;
		}else if(searType.equals("ustate")){
			String hql = "from Cuser where ustate like :sertInfo";
			Query que = this.getCurrentSession().createQuery(hql);
			que.setString("sertInfo",'%'+sertInfo+'%');
			que.setFirstResult((page-1)*rows);
			que.setMaxResults(rows);
			us = que.list();
			return us;
			
		}else{
			String hql = "from Cuser where searType like :sertInfo";
			Query que = this.getCurrentSession().createQuery(hql);
			que.setString("searType", searType);
			que.setString("sertInfo",'%'+sertInfo+'%');
			que.setFirstResult((page-1)*rows);
			que.setMaxResults(rows);
			us = que.list();
			return us;
		}
	}

	@Override
	public int getUsersCountByInfo(String searType, String sertInfo) {
		int i =0;
		if(searType.equals("uid")){
			List<Cuser> us = new ArrayList<Cuser>();
			String hql = "from Cuser where uid like :sertInfo";
			Query que = this.getCurrentSession().createQuery(hql);
			que.setString("sertInfo",'%'+sertInfo+'%');
			us = que.list();
			i = us.size();
			return i;
		}else if(searType.equals("uname")){
			List<Cuser> us = new ArrayList<Cuser>();
			String hql = "from Cuser where uname like :sertInfo";
			Query que = this.getCurrentSession().createQuery(hql);
			que.setString("sertInfo",'%'+sertInfo+'%');
			us = que.list();
			i = us.size();
			return i;
		}else if(searType.equals("utel")){
			List<Cuser> us = new ArrayList<Cuser>();
			String hql = "from Cuser where utel like :sertInfo";
			Query que = this.getCurrentSession().createQuery(hql);
			que.setString("sertInfo",'%'+sertInfo+'%');
			us = que.list();
			i = us.size();
			return i;
		}else if(searType.equals("ustate")){
			List<Cuser> us = new ArrayList<Cuser>();
			String hql = "from Cuser where ustate like :sertInfo";
			Query que = this.getCurrentSession().createQuery(hql);
			que.setString("sertInfo",'%'+sertInfo+'%');
			us = que.list();
			i = us.size();
			return i;
		}else{
			List<Cuser> us = new ArrayList<Cuser>();
			String hql = "from Cuser where searType like :sertInfo";
			Query que = this.getCurrentSession().createQuery(hql);
			que.setString("searType", searType);
			que.setString("sertInfo",'%'+sertInfo+'%');
			us = que.list();
			i = us.size();
			return i;
		}
	}

}
